package com.hlhj.funlike_business.activity;

import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.DetailOrderItemAdp;
import com.hlhj.funlike_business.bean.OrderDetailBean;
import com.hlhj.funlike_business.presenter.OrderDetailPresenter;
import com.hlhj.funlike_business.utils.Tools;
import com.hlhj.funlike_business.views.OrderDetailView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

public class OrderDetailAty extends BaseActivity<OrderDetailView, OrderDetailPresenter> implements OrderDetailView {
    private ImageView img_back, img_icon;
    private TextView tv_orderNum, tv_packgeMoney, tv_takeMoney, tv_manjian, tv_jianMoney, tv_total_price, tv_name, tv_arrivalTime, tv_address, tv_phone, tv_num, tv_total_price1, tv_takeorder, tv_chargeback;
    private RecyclerView recycle_list;
    private DetailOrderItemAdp detailOrderItemAdp;
    private List<OrderDetailBean.DataBean.GoodsBean> goodsBeanList;
    private int order_id;
    private String tel;
    private int status;//状态21配送 30送达 40完成
    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    tv_takeorder.setText("已退单");
                    tv_chargeback.setVisibility(View.GONE);
                    tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
                    break;
                case 2:
                    if (status == 20) {
                        tv_takeorder.setText("立即配送");
                        tv_takeorder.setBackgroundColor(getResources().getColor(R.color.yellow));
                        status = 21;
                    } else if (status == 21) {
                        tv_takeorder.setText("送达");
                        tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
                        status = 30;
                    } else if (status == 30) {
                        tv_takeorder.setText("已完成");
                        tv_chargeback.setVisibility(View.GONE);
                        tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
                        status = 40;
                    }
                    break;
            }
        }
    };

    @Override
    protected int getContentId() {
        return R.layout.aty_orderdetail;
    }

    @Override
    protected void initData() {
        recycle_list = findViewById(R.id.recycle_list);
        recycle_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        goodsBeanList = new ArrayList<>();
        detailOrderItemAdp = new DetailOrderItemAdp(goodsBeanList);

        recycle_list.setAdapter(detailOrderItemAdp);
        recycle_list.setLayoutManager(new LinearLayoutManager(this) {
            //解决scrollerview中的recyclerview滑动不流畅
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    @NotNull
    @Override
    public OrderDetailView bindView() {
        return this;
    }

    @NotNull
    @Override
    public OrderDetailPresenter createPresenter() {
        return new OrderDetailPresenter();
    }

    @Override
    public void initView() {
        img_back = findViewById(R.id.img_back);
        recycle_list = findViewById(R.id.recycle_list);
        tv_orderNum = findViewById(R.id.tv_orderNum);
        tv_packgeMoney = findViewById(R.id.tv_packgeMoney);
        tv_takeMoney = findViewById(R.id.tv_takeMoney);
        tv_manjian = findViewById(R.id.tv_manjian);
        tv_jianMoney = findViewById(R.id.tv_jianMoney);
        tv_total_price = findViewById(R.id.tv_total_price);
        tv_arrivalTime = findViewById(R.id.tv_arrivalTime);
        tv_address = findViewById(R.id.tv_address);
        tv_name = findViewById(R.id.tv_name);
        img_icon = findViewById(R.id.img_icon);
        tv_phone = findViewById(R.id.tv_phone);
        tv_num = findViewById(R.id.tv_num);
        tv_total_price1 = findViewById(R.id.tv_total_price1);
        tv_takeorder = findViewById(R.id.tv_takeorder);
        tv_chargeback = findViewById(R.id.tv_chargeback);

        order_id = getIntent().getIntExtra("order_id", 0);
    }

    @Override
    public void logicStart() {
        getPresenter().LoadOrderDetail(this, order_id);
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.callPhone(OrderDetailAty.this, tel);
            }
        });
        tv_takeorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status == 20) {
                    getPresenter().LoadOrderResult(OrderDetailAty.this, order_id, 21);
                } else if (status == 21) {
                    getPresenter().LoadOrderResult(OrderDetailAty.this, order_id, 30);
                } else if (status == 30) {
                    getPresenter().LoadOrderResult(OrderDetailAty.this, order_id, 40);
                }
            }
        });
        tv_chargeback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().LoadOrderChargeResult(OrderDetailAty.this, order_id);
            }
        });
    }

    @Override
    public void showOrderDetail(OrderDetailBean.DataBean dataBean) {
        tv_orderNum.setText(dataBean.getOrder().getOrder_sn());
        tv_packgeMoney.setText(dataBean.getOrder().getPacking());
        tv_takeMoney.setText(dataBean.getOrder().getFreight());
        tv_manjian.setText(dataBean.getOrder().getReduced_text());
        tv_jianMoney.setText("-￥" + dataBean.getOrder().getReduced_price());
        tv_total_price.setText("￥" + dataBean.getOrder().getPrice());
        tv_total_price1.setText("￥" + dataBean.getOrder().getPrice());
        tv_name.setText(dataBean.getAddress().getName());
        Glide.with(OrderDetailAty.this).load(dataBean.getAddress().getImg()).into(img_icon);
        tv_num.setText("数量: " + dataBean.getOrder().getGoods_num());
        tv_arrivalTime.setText(dataBean.getOrder().getDeliver_time() + "前送达");
        tv_address.setText("地址详情: " + dataBean.getAddress().getAddress_detail());
        tel = dataBean.getAddress().getMobile();
        tv_phone.setText(tel);
        status = dataBean.getOrder().getStatus();

        if (dataBean.getOrder().getStatus() == 20) {
            tv_takeorder.setText("立即接单");
            tv_takeorder.setTextColor(getResources().getColor(R.color.black));
            tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
        } else if (dataBean.getOrder().getStatus() == 21) {
            tv_takeorder.setText("立即配送");
            tv_takeorder.setTextColor(getResources().getColor(R.color.white));
            tv_takeorder.setBackgroundColor(getResources().getColor(R.color.yellow));
        } else if (dataBean.getOrder().getStatus() == 30) {
            tv_takeorder.setText("送达");
            tv_takeorder.setTextColor(getResources().getColor(R.color.black));
            tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
        } else if (dataBean.getOrder().getStatus() == 40) {
            tv_takeorder.setText("已完成");
            tv_chargeback.setVisibility(View.GONE);
            tv_takeorder.setTextColor(getResources().getColor(R.color.black));
            tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
        } else if (dataBean.getOrder().getStatus() == 60) {
            tv_takeorder.setText("已退单");
            tv_chargeback.setVisibility(View.GONE);
            tv_takeorder.setTextColor(getResources().getColor(R.color.black));
            tv_takeorder.setBackgroundColor(getResources().getColor(R.color.tvblue));
        }

        goodsBeanList.addAll(dataBean.getGoods());
        detailOrderItemAdp.notifyDataSetChanged();
    }

    @Override
    public void showResult(String msg) {
        if (msg.contains("退单")) {
            handler.sendEmptyMessage(1);
        } else {
            handler.sendEmptyMessage(2);
        }
        Toast.makeText(OrderDetailAty.this, msg.toString(), Toast.LENGTH_SHORT).show();
    }
}
